﻿using System;
using System.Collections.Generic;
using System.Text;
using XcoAppSpaces.Contracts.Service;
using NUnit.Framework;
using XcoAppSpaces.Contracts.Exceptions;
using XcoAppSpaces.Transport.Sockets;

namespace test.XcoAppSpaces.Transport.Sockets
{
    [TestFixture]
    public class testStringConfig
    {
        [Test]
        public void NoConfig_NoService_loaded()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            configStrings.Add("nothing", "nothing");
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
            Assert.IsNotNull(serviceConfig);
            Assert.IsNull(serviceConfig.Service);
        }
        [Test]
        public void Config_Active_Correct_Service()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            string port = "1234";
            configStrings.Add(ConfigStringInterpreter.ConfigPort, port);
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
            Assert.IsNotNull(serviceConfig.Service);
            Assert.AreEqual(typeof(XcoTCPTransportService), serviceConfig.Service.GetType());
            XcoTCPTransportService service = (XcoTCPTransportService)serviceConfig.Service;
        }
        [Test]
        public void Config_Port_Correct_Only()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            string port = "1234";
            configStrings.Add(ConfigStringInterpreter.ConfigPort, port);
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
            XcoTCPTransportService service = (XcoTCPTransportService)serviceConfig.Service;
            Assert.AreEqual(service.Port.ToString(), port);
        }
        [Test]
        public void Config_Port_Wrong_Only()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            configStrings.Add(ConfigStringInterpreter.ConfigPort, "myport");
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            try
            {
                XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
                Assert.Fail("Missing XcoConfigurationException.");
            }
            catch (XcoConfigurationException)
            {
            }
        }
        [Test]
        public void Config_IsDefault_True_Only()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            configStrings.Add(ConfigStringInterpreter.ConfigIsDefault, "true");
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
            Assert.IsNotNull(serviceConfig.Service);
            Assert.AreEqual(serviceConfig.IsDefault, true);
        }
        [Test]
        public void Config_IsDefault_False_Only()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            configStrings.Add(ConfigStringInterpreter.ConfigIsDefault, "false");
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
            Assert.IsNotNull(serviceConfig.Service);
            Assert.AreEqual(serviceConfig.IsDefault, false);
        }
        [Test]
        public void Config_IsDefault_Wrong_Only()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            configStrings.Add(ConfigStringInterpreter.ConfigIsDefault, "unknown");
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            try
            {
                XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
                Assert.Fail("Missing XcoConfigurationException.");
            }
            catch (XcoConfigurationException)
            {
            }
        }

        [Test]
        public void Config_Name_Correct_Only()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            string name = "myname";
            configStrings.Add(ConfigStringInterpreter.ConfigName, name);
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
            Assert.IsNotNull(serviceConfig.Service);
            Assert.AreEqual(serviceConfig.Name, name);
        }

		[Test]
		public void Config_Lifetime()
		{
			Dictionary<string, string> configStrings = new Dictionary<string, string>();
			configStrings.Add(ConfigStringInterpreter.ConfigLifetime, "10:30");
			IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
			XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
			Assert.IsNotNull(serviceConfig.Service);
			Assert.AreEqual(((XcoTCPTransportService)serviceConfig.Service).ConnectionLifetime, new TimeSpan(0, 10, 30));
		}

        [Test]
        public void Config_AllProperies()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            string port = "1234";
            string name = "myname";
            configStrings.Add(ConfigStringInterpreter.ConfigPort, port);
            configStrings.Add(ConfigStringInterpreter.ConfigName, name);
            configStrings.Add(ConfigStringInterpreter.ConfigIsDefault, "true");
			configStrings.Add(ConfigStringInterpreter.ConfigLifetime, "10:30");
			IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
            XcoTCPTransportService service = (XcoTCPTransportService)serviceConfig.Service;
            Assert.AreEqual(service.Port.ToString(), port);
            Assert.AreEqual(serviceConfig.Name, name);
            Assert.AreEqual(serviceConfig.IsDefault, true);
			Assert.AreEqual(service.ConnectionLifetime, new TimeSpan(0, 10, 30));
        }
    }

}
